package com.rjs.nio.channel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

/**
 * UDP发送端
 * @author LiuZhenghua
 * 2017年12月12日 上午10:40:08
 */
public class DatagramChannelServer {

	public static void main(String[] args) throws IOException {
		DatagramChannel datagramChannel = DatagramChannel.open();
		datagramChannel.socket().bind(new InetSocketAddress(9998));
		datagramChannel.configureBlocking(false);
		
		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
		while(true) {
			SocketAddress receive = datagramChannel.receive(byteBuffer);
			if (receive != null) {
				byteBuffer.flip();
				while(byteBuffer.hasRemaining()) {
					System.out.print((char)byteBuffer.get());
				}
				byteBuffer.clear();
			}
		}
	}
}
